//
//  customObjTransformHTTPRESTObjDelegate.h
//  etionCBLogic
//
//  Created by wjxfhxy on 14-2-21.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import <Foundation/Foundation.h>

@implementation CTransformHTTPRESTObj

//+(NSObject*)JSONObj:(NSDictionary*)JSONObj transformToProtocolObj:(Class)cls
//{
//    return [cls dictionaryObjToProtocolObj:JSONObj];
//}
//
//+(NSDictionary*)JSONObjFromProtocolObj:(NSObject*)protocolObj
//{
//    return [[protocolObj class] protocolObjToDictionary:protocolObj];
//}
//
//+(NSObject*)JSONObj:(NSDictionary*)JSONObj transformToCustomObj:(NSObject*)customObj
//{
//    return [[customObj class] dictionaryToCustomObj:JSONObj];
//}
//
//+(NSDictionary*)JSONObjFromCustomObj:(NSObject*)customObj
//{
//    return [[customObj class] customObjToDictionary:customObj];
//}

+(NSArray*)JSONObjs:(NSArray *)JSONObjs transformToProtocolObj:(Class)cls accordingToColPropertyPair:(XMSQueueDictionary *)qdSet
{
    return [CTransformHTTPRESTObj JSONObjs:JSONObjs transformToProtocolObj:cls accordingToColPropertyPair:qdSet complexType:^id(NSString *key, ENSObjectPropertyType propertyType, id complexType)
    {
        return nil;
    }];
}

+(NSArray*)JSONObjs:(NSArray*)JSONObjs transformToProtocolObj:(Class)cls accordingToColPropertyPair:(XMSQueueDictionary *)qdSet complexType:(id (^)(NSString *, ENSObjectPropertyType, id))complexType
{
    NSMutableArray *ar = nil;
    if(JSONObjs.count>0)
    {
        ar = [[NSMutableArray new]autorelease_mm];
        for(NSDictionary *obj in JSONObjs)
        {
            id o = [[cls new]autorelease_mm];
            [ar addObject:o];

            for(CObjTransformPropertyPair *pair in qdSet)
            {
                if([o respondsToSelector:NSSelectorFromString(pair.idValue)] == NO)
                    continue;
                
                id value = [obj objectForKey:pair.idKey];
                if([value isKindOfClass:[NSNull class]])
                {
                    continue;
                }
                switch (pair.propertyType)
                {
                    case ENSObjectPropertyTypeNSString:
                    {
                        value = value;
                        break;
                    }
                    case ENSObjectPropertyTypeNSDate:
                    {
                        if(value != nil)
                        {
                            value = [NSDate formatNSStringDate:value format:yMdHms];
                            if(value == nil)
                            {
                                value = [obj objectForKey:pair.idKey];
                                value = [NSDate formatNSStringDate:value format:yMdHmsS];
                                if(value == nil)
                                {
                                    value = [obj objectForKey:pair.idKey];
                                    value = [NSDate formatNSStringDate:value format:yMd];
                                    if(value == nil)
                                    {
                                        value = [obj objectForKey:pair.idKey];
                                        value = [NSDate formatNSStringDate:value format:yMdHm];
                                    }
                                }
                            }
                        }
                        else
                            value = [NSDate dateWithTimeIntervalSince1970:0];
                        break;
                    }
                    case ENSObjectPropertyTypeNumberInt:
                    {
                        if([value isKindOfClass:[NSString class]] && ((NSString*)value).length <= 0)
                            value = nil;
                        else
                            value = [XMSNumberInt numberIntWithIntValue:[value integerValue]];
                        break;
                    }
                    case ENSObjectPropertyTypeNumberLongLong:
                    {
                        if([value isKindOfClass:[NSString class]] && ((NSString*)value).length <= 0)
                            value = nil;
                        else
                            value = [XMSNumberLongLong numberLongLongWithLongLongValue:[value longLongValue]];
                        break;
                    }
                    case ENSObjectPropertyTypeNumberFloat:
                    {
                        if([value isKindOfClass:[NSString class]] && ((NSString*)value).length <= 0)
                            value = nil;
                        else
                            value = [XMSNumberFloat numberFloatWithFloatValue:[value floatValue]];
                        break;
                    }
                    case ENSObjectPropertyTypeNumberDouble:
                    {
                        if([value isKindOfClass:[NSString class]] && ((NSString*)value).length <= 0)
                            value = nil;
                        else
                            value = [XMSNumberDouble numberDoubleWithDoubleValue:[value doubleValue]];
                        break;
                    }
                    case ENSObjectPropertyTypeArray:
                    case ENSObjectPropertyTypeDictionary:
                    case ENSObjectPropertyTypeID:
                    {
                        value = complexType(pair.idKey,pair.propertyType,value);
                        break;
                    }
                    case ENSObjectPropertyTypeBOOL:
                    {
                        value = value;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                if(value != nil && [value isKindOfClass:[NSNull class]] == NO)
                    [o setValue:value forKey:pair.idValue];
            }
        }
    }
    
    return ar;
}

static XMSQueueDictionary *s_jsonpair = nil;

+ (void)registerTransformObjKey:(NSString *)key Cls:(Class)cls transformPropertyPair:(XMSQueueDictionary *)qdPair
{
    if (s_jsonpair == nil)
        s_jsonpair = [XMSQueueDictionary new];
    [s_jsonpair setValue:[CTransformHTTPRESTObj registerJSONObjectClass:cls JSONKeyPair:qdPair] forKey:key];
}

+ (XMSQueueDictionary*)registerJSONObjectClass:(Class)cls JSONKeyPair:(XMSQueueDictionary *)qdPair
{
    for (NSUInteger i = 0; i < [qdPair count]; i++)
    {
        CObjTransformPropertyPair *pair = (CObjTransformPropertyPair *) [qdPair kvPairAtIndex:i];
        ENSObjectPropertyType type = [NSObject NSObjectPropertyType:[NSObject getPropertyType:cls property:pair.idValue]];
        pair.propertyType = type;
    }
    return qdPair;
}

+ (XMSQueueDictionary *)getTransformPropertyPair:(NSString *)key transformJSONKey:(NSArray *)JSONKeys
{
    return [CTransformHTTPRESTObj JSONObjectMap:[s_jsonpair getValueForKey:key] propertyWithJSONKey:JSONKeys];
}

+ (XMSQueueDictionary *)JSONObjectMap:(XMSQueueDictionary*)qdMap propertyWithJSONKey:(NSArray *)JSONKeys
{
    XMSQueueDictionary *kv = [[[XMSQueueDictionary alloc] initWithStoreClass:[CObjTransformPropertyPair class]] autorelease_mm];
    if (JSONKeys.count > 0)
    {
        [JSONKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
        {
            for (NSUInteger i = 0; i < [qdMap count]; i++)
            {
                CObjTransformPropertyPair *pair = (CObjTransformPropertyPair *) [qdMap kvPairAtIndex:i];
                if ([(NSString *) obj isEqualToString:(NSString *) pair.idKey] == YES)
                {
                    [kv addKvPair:pair];
                    break;
                }
            }
        }];
    }
    else
    {
        for (NSUInteger i = 0; i < [qdMap count]; i++)
        {
            CObjTransformPropertyPair *pair = (CObjTransformPropertyPair *) [qdMap kvPairAtIndex:i];
            [kv addKvPair:pair];
        }
    }
    return kv;
}

@end
